package com.distortiongames.level27.generation
{
	
	/**
	 * ...
	 * @author Jefferson Wagner
	 */
	public class Map
	{
		public static const EMPTY:int = 0, START:int = 1, END:int = 2, CORRIDOR:int = 3;
		public static const ROOM_START:int = 4;
		private var map:Vector.<Vector.<int>>;
		
		public function get width():int
		{
			return map[0].length;
		}
		
		public function get height():int
		{
			return map.length;
		}
		
		public function Map(width:int, height:int)
		{
			map = new Vector.<Vector.<int>>();
			for (var x:int = 0; x < height; x++)
			{
				map[x] = new Vector.<int>();
			}
			for (var y:int = 0; y < height; y++)
			{
				for (x = 0; x < width; x++)
				{
					map[y].push(0);
				}
			}
		}
		
		public function trim() : void {
			var upperBound:int = int.MAX_VALUE, rightBound:int = 0, lowerBound:int = 0, leftBound:int = int.MAX_VALUE;
			for (var y:int = 0; y < height; y++)
			{
				for (var x:int = 0; x < width; x++)
				{
					if (map[y][x] != Map.EMPTY) {
						if (x < upperBound)
							upperBound = x;
						else if (x > lowerBound)
							lowerBound = x;
						if (y < leftBound)
							leftBound = y;
						else if (y > rightBound)
							rightBound = y;
					}
				}
			}
			var newHeight:int = lowerBound - upperBound;
			var newWidth:int = rightBound - leftBound;
			var mapTemp:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
			for (x = 0; x < newHeight; x++)
			{
				mapTemp[x] = new Vector.<int>();
			}
			var yy:int = 0;
			for (y = upperBound; y < lowerBound; y++, yy++)
			{
				for (x = leftBound; x < rightBound; x++)
				{
					mapTemp[yy].push(map[y][x]);
				}
			}
			trace(upperBound)
			trace(lowerBound);
			map = mapTemp;
		}
		
		public function getTile(point:Point):int
		{
			var value:int = -1;
			if (point.x >= 0 && point.y >= 0 && point.x < width && point.y < height)
				value = map[point.y][point.x];
			return value;
		}
		
		public function setTile(point:Point, value:int):void
		{
			map[point.y][point.x] = value;
		}
		
		public function getNumberOfSurroundingTiles(point:Point):int
		{
			var result:int = 0;
			if (getTile(point.added(new Point(0, -1))) != 0)
				result++;
			if (getTile(point.added(new Point(0, 1))) != 0)
				result++;
			if (getTile(point.added(new Point(1, 0))) != 0)
				result++;
			if (getTile(point.added(new Point(-1, 0))) != 0)
				result++;
			return result;
		
		}
		
		public function getNorthValue(point:Point):int
		{
			return getTile(point.added(new Point(0, -1)));
		}
		
		public function getSouthValue(point:Point):int
		{
			return getTile(point.added(new Point(0, 1)));
		}
		
		public function getEastValue(point:Point):int
		{
			return getTile(point.added(new Point(1, 0)));
		}
		
		public function getWestValue(point:Point):int
		{
			return getTile(point.added(new Point(-1, 0)));
		}
		
		public function toString():String
		{
			var string:String = "";
			for (var y:int = 0; y < map.length; y++)
			{
				for (var x:int = 0; x < map[y].length; x++)
				{
					string += map[y][x] + " ";
				}
				string += "\n";
			}
			return string.toString();
		}
	
	}
}